package com.ybk.zs.controller;

import com.ybk.zs.common.utils.LoggerUtil;
import com.ybk.zs.common.utils.StringUtils;
import com.ybk.zs.controller.resp.BaseResp;
import com.ybk.zs.controller.resp.QueryDayCostResp;
import com.ybk.zs.domain.*;
import com.ybk.zs.domain.ex.CustomerCostEx;
import com.ybk.zs.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Administrator on 2016/9/6.
 */
@Controller
@RequestMapping(value = "/customerCost/")
public class CustomerCostController {
    @Autowired
    ICustomerCostService customerCostService;

    @Autowired
    ICostScoreService costScoreService;

    @Autowired
    ICustomerService customerService;

    @Autowired
    ICustomerCostExService customerCostExService;

    @Autowired
    IScoreCostService scoreCostService;

    @Autowired
    ICustomerScoreService customerScoreService;

    /**
     * 按日期加载消费记录
     */
    @RequestMapping(value = "queryDayCost", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public QueryDayCostResp queryDayCost(String date) {
        QueryDayCostResp resp = new QueryDayCostResp();

        try {
            List<CustomerCostEx> list = customerCostExService.queryDayCost(date);

            resp.setCostList(list);
            resp.setResultCode("001");
            resp.setResultMsg("处理成功");
            return resp;
        } catch (Exception e) {
            LoggerUtil.error(e, "查找今日消费情况出现异常");
            resp.setResultCode("003");
            resp.setResultMsg(StringUtils.getUTF8CharsetString("查找今日消费情况出现异常"));
            return resp;
        }
    }

    /**
     * 新增一条消费记录
     */
    @RequestMapping(value = "add", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public BaseResp add(Integer customerNum, Double cost, Double score) {
        LoggerUtil.info("新建消费记录");
        BaseResp resp = new BaseResp();
        try {

            //判断该会员是否存在
            CustomerExample example = new CustomerExample();
            example.or().andNumEqualTo(customerNum);
            int customerCount = customerService.countByExample(example);
            if (customerCount == 0) {
                resp.setResultCode("002");
                resp.setResultMsg(StringUtils.getUTF8CharsetString("该会员不存在, customerNum: " + customerNum));
                return resp;
            }

            //会员消费已累积的积分

            //检查会员是否还剩积分
            CustomerScoreExample example1 = new CustomerScoreExample();
            example1.or().andCustomerNumEqualTo(customerNum);
            List<CustomerScore> customerScores = customerScoreService.selectByExample(example1);
            if ((score!=null&&score>0)&&(customerScores == null || customerScores.size() <= 0||customerScores.get(0).getCustomerScore()<=0)) {//会员还未积分
                resp.setResultCode("002");
                resp.setResultMsg(StringUtils.getUTF8CharsetString("会员的积分为 0 , customerNum: " + customerNum));
                return resp;
            }
            if (score != null && score > 0) {

                if(customerScores.get(0).getCustomerScore()< score){
                    resp.setResultCode("002");
                    resp.setResultMsg(StringUtils.getUTF8CharsetString("会员的积分为: "+customerScores.get(0).getCustomerScore()+", customerNum: " + customerNum));
                    return resp;
                }
                ScoreCost scoreCost = new ScoreCost();
                scoreCost.setId(StringUtils.getShortUuid());
                scoreCost.setCustomerNum(customerNum);
                scoreCost.setScoreCost(score);
                scoreCost.setIsDelete(0);
                scoreCostService.insert(scoreCost);
            }

            //生成消费记录 和 累积积分
            if (cost != null && cost > 0) {
                String costUUid = StringUtils.getShortUuid();
                CustomerCost record = new CustomerCost();

                record.setCostId(costUUid);
                record.setCustomerNum(customerNum);
                record.setCost(cost);
                record.setIsDelete(0);

                customerCostService.insert(record);

                //针对该笔消费对消费会员进行积分
                CostScore costScore = new CostScore();
                costScore.setCostId(costUUid);
                costScore.setIsDelete(0);
                costScore.setCustomerNum(customerNum);
                costScore.setScore(cost); //消费1块积分1分
                costScoreService.insert(costScore);

                //针对该笔消费对其上线会员积分
                //获取其上线会员
                CustomerExample customerExample = new CustomerExample();
                customerExample.or().andNextNumEqualTo(customerNum);
                List<Customer> list = customerService.selectByExample(customerExample);

                if (list != null && list.size() > 0) {
                    CostScore preCostScore = new CostScore();
                    preCostScore.setCostId(costUUid);
                    preCostScore.setIsDelete(0);
                    preCostScore.setCustomerNum(list.get(0).getNum());
                    preCostScore.setScore(cost / 3); //消费1块积分0.3分
                    costScoreService.insert(preCostScore);

                    //针对该笔消费对其上上线会员积分
                    //获取其上上线会员
                    CustomerExample customerExample1 = new CustomerExample();
                    customerExample1.or().andNextNumEqualTo(list.get(0).getNum());
                    List<Customer> list1 = customerService.selectByExample(customerExample1);

                    if (list1 != null && list1.size() > 0) {
                        CostScore preCostScore1 = new CostScore();
                        preCostScore1.setCostId(costUUid);
                        preCostScore1.setIsDelete(0);
                        preCostScore1.setCustomerNum(list1.get(0).getNum());
                        preCostScore1.setScore(cost / 5); //消费1块积分0.2分
                        costScoreService.insert(preCostScore1);
                    }
                }
            }

            resp.setResultCode("001");
            resp.setResultMsg("新建消费记录成功");
            return resp;
        } catch (Exception e) {
            LoggerUtil.error(e, "新增消费记录出现异常", "customerNum:" + customerNum);
            resp.setResultCode("003");
            resp.setResultMsg(StringUtils.getUTF8CharsetString("新增消费记录出现异常,customerNum:" + customerNum));
            return resp;
        }
    }

    /**
     * 删除一条消费记录
     */
    @RequestMapping(value = "delete", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public BaseResp delete() {
        BaseResp resp = new BaseResp();
        return resp;
    }
}
